package cn.nohan.funplay.module.member.mongo.config;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.nohan.funplay.module.member.mongo.service.notify.message.*;
import com.mongoplus.mapping.MongoConverter;
import com.mongoplus.strategy.conversion.ConversionStrategy;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.stereotype.Component;

/**
 * @author caozhipeng
 * @date 2025/8/13
 */
@Slf4j
@Component
public class MessageConversionStrategy implements ConversionStrategy<Message> {
    @Override
    public Message convertValue(Object fieldValue, Class<?> fieldType, MongoConverter mongoConverter) throws IllegalAccessException {
        String classType=null;
        if(ObjUtil.isNotNull(fieldValue)&&(fieldValue.getClass().equals(Document.class))){
            Document document=(Document)fieldValue;
             classType=document.getString("classType");
            if(StrUtil.isEmpty(classType)){
                return null;
            }
        }

        if(StrUtil.equals(classType,CommonMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),CommonMessage.class);
        }
        if(StrUtil.equals(classType,PaymentMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),PaymentMessage.class);
        }
        if(StrUtil.equals(classType, AbdicateMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue), AbdicateMessage.class);
        }
        if(StrUtil.equals(classType, ActivityMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),ActivityMessage.class);
        }
        if(StrUtil.equals(classType,CommonMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),CommonMessage.class);
        }
        if(StrUtil.equals(classType,DynamicMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),DynamicMessage.class);
        }
        if(StrUtil.equals(classType,GroupMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),GroupMessage.class);
        }
        if(StrUtil.equals(classType,LogisticsMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),LogisticsMessage.class);
        }
        if(StrUtil.equals(classType,OrderMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),OrderMessage.class);
        }
        if(StrUtil.equals(classType,RefundMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),RefundMessage.class);
        }
        if(StrUtil.equals(classType,ReportMessage.class.getTypeName())){
            return JSONUtil.toBean(JSONUtil.toJsonStr(fieldValue),ReportMessage.class);
        }
//        消息类型，后续新增在此添加........

        return null;
    }
}
